//
//  HomePageView.m
//  MindBox
//
//  Created by MS on 16-1-1.
//  Copyright (c) 2016年 Lemon. All rights reserved.
//

#import "HomePageView.h"
#import "UIView+Frame.h"
#import "BottomView.h"
#import "GSAdsView.h"
#import "Bonus_banner_View.h"
#import "Pop_Cat_View.h"
#import "Gift_TopicsView.h"
#import "PmtView.h"
#import "WeekView.h"
#import "HomeGoodsCell.h"
#define MARGIN 10
@interface HomePageView ()<UICollectionViewDelegateFlowLayout,UICollectionViewDataSource,GsAdsViewDelegate,bonus_banner_ViewDelegate,Pop_Cat_ViewDelegate,Gift_TopicsViewDelegate,PmtViewDelegate,WeekViewDlegate>


@property (nonatomic,weak) GSAdsView * adsView;
@property (nonatomic,weak) GSAdsView * footerAdsView;
@property (nonatomic,weak) Bonus_banner_View * bonus_banner_View;
@property (nonatomic,weak) Pop_Cat_View * pop_cat_View;
@property (nonatomic,weak) Gift_TopicsView * gift_TopicsView;
@property (nonatomic,weak) PmtView * pmtView;
@property (nonatomic,weak) BottomView * bottomView;
@property (nonatomic,weak) WeekView * weekView;
@end

@implementation HomePageView
{
    NSMutableArray * _headerArr;
    NSMutableArray * _footerArr;
    
}

-(void)setGoodsData:(NSArray *)goodsData
{
    _goodsData = goodsData;

    [self.collectionView reloadData];

}

-(void)setData:(Data *)data
{
    _data = data;
    
    [_headerArr removeAllObjects];
    [_footerArr removeAllObjects];
    
    //1.设置头部广告条
    for ( Banner * ban in data.banner) {
        NSString * image_url = ban.image_url;
        [_headerArr addObject:image_url];
    }
    self.adsView.imagesArray = _headerArr;
//
    
    //2.设置头部3个活动栏
    self.bonus_banner_View.bonus_banner = data.bonus_banner;
 
    //3.设置gift_topicsView
//    self.gift_TopicsView.gift_topic = data.gift_topics;
    
    //4.设置PmtView
    self.pmtView.pmts = data.pmt;
    
    //5.设置WeekView
    self.weekView.weeks =data.week;
    
    //6.设置主题馆
    self.pop_cat_View.pop_cat = data.pop_cat;
 
    //7.设置底部广告条
    for (Tail_Banner * tail_banner in data.tail_banner) {
        NSString * image_url = tail_banner.image_url;
        [_footerArr addObject:image_url];
        
    }
    self.footerAdsView.hidden =YES;
    
    [self.collectionView reloadData];
}

#pragma mark 懒加载广告视图adsView、footerAdsView

-(GSAdsView *)adsView
{
    if (_adsView==nil) {
        
       GSAdsView * view = [self creatAdsView];
        _adsView = view;
        _adsView.intervalTime = 5;
        _adsView.frame = CGRectMake(0, 0, self.width, self.height*0.25);
        _adsView.tag =0;
        _adsView.delegate = self;
    }
    return _adsView;
}

-(GSAdsView *)footerAdsView
{
    if (_footerAdsView==nil) {
      GSAdsView * view = [self creatAdsView];
        _footerAdsView = view;
        _footerAdsView.intervalTime = 5;
        _footerAdsView.frame = CGRectMake(0, CGRectGetMaxY(_pop_cat_View.frame) + MARGIN, self.width,0);

        _footerAdsView.tag=1;
        _footerAdsView.delegate = self;
        
        BottomView * bottomView = [BottomView bottomViewWithXib];
        self.bottomView = bottomView;
        bottomView.frame =CGRectMake(0, CGRectGetMaxY(_footerAdsView.frame), self.width,30);
        [self.collectionView addSubview:bottomView];
    }
    return _footerAdsView;
}

-(GSAdsView *)creatAdsView
{
    GSAdsView * view = [[GSAdsView alloc] init];
    [self.collectionView addSubview:view];
    view.pageTintColor = [UIColor whiteColor];
    view.currentPageTintColor = [UIColor redColor];
    return view;
}

#pragma mark 懒加载Bonus_banner_View
-(Bonus_banner_View *)bonus_banner_View
{
    if (_bonus_banner_View==nil) {
        Bonus_banner_View * view = [[Bonus_banner_View alloc]init];
        [self.collectionView addSubview:view];
        _bonus_banner_View = view;
        _bonus_banner_View.delegate = self;
        _bonus_banner_View.frame = CGRectMake(0, CGRectGetMaxY(_adsView.frame), self.width, _adsView.height*0.6);
    }
    return _bonus_banner_View;
}

#pragma mark 懒加载gift_TopicsView
-(Gift_TopicsView *)gift_TopicsView
{
    if (_gift_TopicsView==nil) {
        Gift_TopicsView *  view = [[Gift_TopicsView alloc] init];
        [self.collectionView addSubview:view];
        _gift_TopicsView = view;
        _gift_TopicsView.delegate = self;
        _gift_TopicsView.frame =CGRectMake(0, CGRectGetMaxY(_bonus_banner_View.frame)+MARGIN, self.width, 150);//150
    }
    return _gift_TopicsView;
}

#pragma mark 懒加载PmtView
-(PmtView *)pmtView
{
    if (_pmtView==nil) {
        PmtView * view =[[PmtView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_bonus_banner_View.frame)+MARGIN, self.width, 190)];
        [self.collectionView addSubview:view];
        _pmtView = view;
        _pmtView.delegate = self;
    }
    return _pmtView;
}

#pragma mark 懒加载WeekView
-(WeekView *)weekView
{
    if (_weekView==nil) {
        WeekView * view = [WeekView weekViewWithXib];
        view.frame =CGRectMake(0, CGRectGetMaxY(_pmtView.frame)+MARGIN, self.width, 230);
         [self.collectionView addSubview:view];
        _weekView = view;
        _weekView.delegate = self;
    }
    return _weekView;
}

#pragma mark 懒加载Pop_Cat_View
-(Pop_Cat_View *)pop_cat_View
{
    if (_pop_cat_View==nil) {
        Pop_Cat_View * popview = [[Pop_Cat_View alloc] init];
        [self.collectionView addSubview:popview];
        _pop_cat_View = popview;
        _pop_cat_View.delegate =self;
        _pop_cat_View.frame = CGRectMake(0, CGRectGetMaxY(_weekView.frame)+MARGIN, self.width, 300);
    }
    return _pop_cat_View;
}

#pragma mark adsView广告条代理方法
-(void)gsAdsViewTouch:(GSAdsView *)GSAdsView Index:(NSInteger)index
{
    if (GSAdsView.tag==0) {
        Banner * banner = _data.banner[index];
        
        if ([self.delegate respondsToSelector:@selector(homePageBannerDidClick:banner:index:)]) {
            [self.delegate homePageBannerDidClick:self banner:banner index:index];
        }
    }
    
    if (GSAdsView.tag==1) {
        Tail_Banner * tail_banner = _data.tail_banner[index];
        
        if ([self.delegate respondsToSelector:@selector(homePageTail_BannerDidClick:tail_banner:index:)]) {
            [self.delegate homePageTail_BannerDidClick:self tail_banner:tail_banner index:index];
        }

    }
    
}

#pragma mark Bonus_banner_View 代理方法
-(void)bonus_banner_DidClick:(Bonus_banner_View *)bonusView bonus_banner:(Bonus_Banner *)bonus_banner index:(NSInteger)index
{
    if ([self.delegate respondsToSelector:@selector(homePageBonus_BannerDidClick:bonus_banner:index:)]) {
        [self.delegate homePageBonus_BannerDidClick:self bonus_banner:bonus_banner index:index];
    }
}

#pragma mark Gift_topics_View 代理方法
-(void)gift_TopicsViewDidClick:(Gift_TopicsView *)gift_topicsView gift_topic:(Gift_Topics *)gift_topic index:(NSInteger)index
{
    if ([self.delegate respondsToSelector:@selector(homePageGift_TopicsViewDidClick:gift_Topics:index:)]) {
        
        [self.delegate homePageGift_TopicsViewDidClick:self gift_Topics:gift_topic index:index];
    }
    
}

#pragma mark PmtView 代理方法
-(void)pmtViewGoodsDidClick:(PmtView *)pmtView pmt:(Pmt *)pmt index:(NSInteger)index
{
    if ([self.delegate respondsToSelector:@selector(homePmtViewDidClick:pmt:index:)]) {
        
        [self.delegate homePmtViewDidClick:self pmt:pmt index:index];
    }
    
}

-(void)pmtAllBtnDidClick:(PmtView *)pmtView
{
    if ([self.delegate respondsToSelector:@selector(homePmtViewDidClick:pmt:index:)]) {
        
        [self.delegate homePmtViewDidClick:self pmt:nil index:-1];
    }

}

#pragma mark WeekView 代理方法
-(void)weekViewDidClick:(WeekView *)weekView week:(Week *)week index:(NSInteger)index
{
    if ([self.delegate respondsToSelector:@selector(homeWeekViewDidClick:week:index:)]) {
        [self.delegate homeWeekViewDidClick:self week:week index:index];
    }
}


#pragma mark Pop_cat_view 代理方法
-(void)pop_cat_ViewDidClick:(Pop_Cat_View *)pop_cat_view pop_cat:(Pop_Cat *)pop_cat index:(NSInteger)index
{
    if ([self.delegate respondsToSelector:@selector(homePagePop_CatDidClick:pop_cat:index:)]) {
        [self.delegate homePagePop_CatDidClick:self pop_cat:pop_cat index:index];
    }
    
    
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        _headerArr = [NSMutableArray array];
        _footerArr =[NSMutableArray array];

        UICollectionViewFlowLayout * viewLayout = [[UICollectionViewFlowLayout alloc] init];
        viewLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        viewLayout.minimumLineSpacing = 10;
        viewLayout.minimumInteritemSpacing = 0;
        
        UICollectionView * collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:viewLayout];
        collectionView.delegate = self;
        collectionView.dataSource = self;
       
        self.collectionView = collectionView;

        collectionView.backgroundColor = [UIColor colorWithRed:0.97f green:0.97f blue:0.97f alpha:1.00f];
        [self addSubview:collectionView];
        
    }
    return self;
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.goodsData.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HomeGoodsCell * cell = [HomeGoodsCell cellWithcollectionView:collectionView cellForItemAtIndexPath:indexPath];
    
    cell.goodsData = self.goodsData[indexPath.item];
    
    return cell;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
 
    return CGSizeMake((self.width - 30)/2, 220);
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(CGRectGetMaxY(_bottomView.frame), 10, 0, 10);
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if ([self.delegate respondsToSelector:@selector(homePageDidScroll:)]) {
        [self.delegate homePageDidScroll:self];
    }
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.delegate respondsToSelector:@selector(homeGoodsDidClick:goods:index:)]) {
        
        [self.delegate homeGoodsDidClick:self goods:self.goodsData[indexPath.item] index:indexPath.item];
    }
}


@end
